package com.github.aloxc.plugin.restplus.listener;

import com.github.aloxc.plugin.restplus.component.*;
import com.github.aloxc.plugin.restplus.entity.SettingFormType;
import com.github.aloxc.plugin.restplus.entity.SettingListenerContext;
import com.github.aloxc.plugin.restplus.entity.SettingType;
import com.github.aloxc.plugin.restplus.i18n.I18Bundle;
import com.intellij.openapi.project.Project;
import com.intellij.ui.components.JBPanel;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * 设置的dialog，打开dialog会默认加载全局配置，所有点击全局按钮和环境的处理都在这
 * @author leero
 */
public class SettingListener extends MouseAdapter implements ActionListener {
    private Project project;
    private SettingForm settingForm;
    private SettingListenerContext context;
    public SettingListener(Project project) {
        this.project = project;
        context = new SettingListenerContext(SettingFormType.GLOBAL,null);
    }



    public SettingListener(SettingForm settingForm,SettingListenerContext context) {
        this.settingForm = settingForm;
        this.project = settingForm.getProject();
        this.context = context;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        settingForm = new SettingForm(project);
        //默认显示全局设置变量列表
        context = new SettingListenerContext(SettingFormType.GLOBAL,null);
        this.actionPerformed(null);

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        GridLayoutManager layoutManager = new GridLayoutManager(1, 1);
        layoutManager.setHGap(1);
        layoutManager.setVGap(1);
        layoutManager.setMargin(new Insets(0,0,0,0));
        JBPanel right = null;
//        Dimension size = new Dimension(805, 570);
        String title = null;
        switch (context.getSettingFormType()){
            case ENVIRONMENT:
                title = (String) context.getContext();
                settingForm.getDialog().setTitle(title);
                right = new Variate(SettingType.ENVIRONMENT,project,settingForm.getDialog(),context);
                break;
            case GLOBAL:
                right = new Variate(SettingType.GLOBAL,project,settingForm.getDialog(),context);
                title = I18Bundle.getMessage("com.github.aloxc.plugin.restplus.setting.global.title");
                break;
            case YAPI:
                right = new YapiSettingForm(project,context);
                title = I18Bundle.getMessage("com.github.aloxc.plugin.restplus.setting.yapi.title");
                break;
            case HELP:
                right = new HelpForm(project,context);
                title = I18Bundle.getMessage("com.github.aloxc.plugin.restplus.toolbar.help.text");
            default:
        }
        ISettingPanel settingPanel = (ISettingPanel)right;
        settingPanel.setDialog(settingForm.getDialog());
        settingForm.getDialog().setTitle(title);

        Dimension size = new Dimension(-1, -1);
        if(settingForm.getRightPane().getComponentCount() > 0) {
            settingForm.getRightPane().remove(0);
        }
        settingForm.getRightPane().add(right,
                new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_NORTHWEST, GridConstraints.FILL_BOTH,
                        GridConstraints.FILL_NONE,
                        GridConstraints.FILL_NONE,
                        size, size, size));
        settingForm.updateUI();
        settingForm.getDialog().setVisible(true);
    }
}
